<template>
  <div class="device-type-list-container">
    <edit
      ref="editComponent"
      @refresh="search"
    ></edit>
    <header-search-form
      :searchFormData="searchForm"
      @search="search"
    />
    <lan-table
      size="small"
      :config="dataTable"
      :tableData="tableData"
      @search="search"
    >
      <template #type="scope">
        <el-input v-model="scope.row.type"></el-input>
      </template>
      <template #imageUrl="scope">
        <el-image
          style="width: 100px; height: 100px"
          :fit="cover"
          v-if="!scope.row.imageUrl==''"
          :src="scope.row.imageUrl"
          :preview-src-list="[scope.row.imageUrl]"
        >
          <div
            slot="error"
            class="image-slot"
          >
            <span style="display:block;margin-top:45px">加载失败</span>
          </div>
        </el-image>
      </template>
    </lan-table>
  </div>
</template>

<script>
import lanTable from '../../../components/lanTable';
import HeaderSearchForm from './headerSearchForm.vue';
import moment from 'moment';
import Edit from './edit';
import { MomentFormat, SortType } from '../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { getProductTypeListApi, deleteProductTypeApi } from '../../../api/productType';
import { parseError } from '../../../utils';

export default {
  components: {
    Edit,
    lanTable,
    HeaderSearchForm
  },
  data() {
    return {
      dataTable: {
        pageSize: 10,
        totalCount: 0,
        currentPage: 1,
        pageSizes: [5, 10, 20, 50, 100],
        sort: {
          orderBy: 'createdAt',
          sortType: SortType.DESC
        },
        columnList: [
          {
            name: 'index',
            label: '',
            type: 'index'
          },
          {
            name: 'imageUrl',
            label: '图片',
            type: 'expand'
          },
          {
            name: 'name',
            label: '名称'
          },
          {
            name: 'description',
            label: '备注'
          },
          {
            name: 'createdAt',
            label: '创建时间',
            sortable: true
          },
          {
            name: 'updateAt',
            label: '更新时间'
          },
          {
            type: 'button',
            label: '操作',
            buttons: [
              {
                isShow: () => this.userGuardList.includes(this.guard.ProductTypeManage),
                type: 'text',
                text: '编辑',
                size: 'small',
                method: row => {
                  this.showEdit(row);
                }
              },
              {
                isShow: () => this.userGuardList.includes(this.guard.ProductTypeManage),
                type: 'text',
                text: '删除',
                size: 'small',
                method: row => {
                  this.remove(row.uuid, row.name);
                }
              }
            ]
          }
        ]
      },
      searchForm: {
        btnLoading: false,
        name: ''
      },
      tableData: [],
      deviceParameterList: []
    };
  },
  created() {
    this.search();
  },
  methods: {
    search() {
      this.searchForm.btnLoading = true;
      getProductTypeListApi(this.dataTable.currentPage - 1, this.dataTable.pageSize, this.dataTable.sort.sortType, this.dataTable.sort.orderBy, this.searchForm.name)
        .then(({ data, total_count }) => {
          this.dataTable.totalCount = total_count;
          this.tableData = data.map(TypeList => {
            return {
              uuid: TypeList.uuid,
              name: TypeList.name,
              description: TypeList.description,
              imageUrl: TypeList.image_url,
              createdAt: moment(TypeList.created_at).format(MomentFormat),
              updateAt: moment(TypeList.update_at).format(MomentFormat)
              // showViewer: false
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.searchForm.btnLoading = false));
    },
    showEdit(row) {
      this.$refs.editComponent.showDialog(row);
    },
    remove(uuid, name) {
      MessageBox.confirm(`确定删除产品类型${name}吗？`, '删除提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          deleteProductTypeApi(uuid)
            .then(() => Message.success('删除成功'))
            .catch(error => Message.error(parseError(error)))
            .finally(() => this.search());
        })
        .catch(() => {});
    }
  }
};
</script>
<style lang="scss" scoped>
.device-type-list-container {
  padding: 10px;
}
</style>
